package com.qi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qi.common.RoleKey;
import com.qi.pojo.dto.UpdateUserBody;
import com.qi.pojo.entity.User;
import com.qi.pojo.vo.AjaxResult;
import com.qi.pojo.vo.UserDetails;
import com.qi.services.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/user")
@Tag(name = "用户管理")
public class UserController {

    @Resource
    private IUserService userService;

    @SaCheckLogin
    @Operation(summary = "获取当前登录用户的详细信息")
    @GetMapping
    public AjaxResult getCurrentLoginUser() {
        UserDetails curUser = userService.getCurUser();
        return AjaxResult.success(curUser);
    }

    @SaCheckRole(RoleKey.社团管理员)
    @Operation(summary = "获取用户详细信息")
    @GetMapping("/getUserDetails/{userId}")
    public AjaxResult getUserDetails(@PathVariable("userId") Long userId) {
        UserDetails userDetails = userService.getUserDetailsByUserId(userId);
        return AjaxResult.success(userDetails);
    }

    @SaCheckRole(RoleKey.超级管理员)
    @Operation(summary = "获取用户列表")
    @GetMapping("/userList")
    public AjaxResult userList(@RequestParam(required = false) String name,
                               @RequestParam(required = false) String gender) {

        List<User> userList = userService.userList(name, gender);
        return AjaxResult.success(userList);
    }

    @SaCheckRole(RoleKey.社团管理员)
    @Operation(summary = "分页获取社员列表")
    @GetMapping("/userListPage/{page}/{size}")
    public AjaxResult userListPage(@RequestParam(required = false) String name,
                                      @RequestParam(required = false) String gender,
                                      @PathVariable("page") Long page,
                                      @PathVariable("size") Long size) {

        IPage<User> userListPage = userService.userListPage(page, size, name, gender);
        return AjaxResult.success(userListPage);
    }

    @SaCheckRole(RoleKey.社团管理员)
    @Operation(summary = "获取社员列表")
    @GetMapping("/nativeUserList")
    public AjaxResult nativeUserList(@RequestParam(required = false) String name,
                                   @RequestParam(required = false) String gender) {

        List<User> userList = userService.nativeUserList(name, gender);
        return AjaxResult.success(userList);
    }

    @SaCheckLogin
    @Operation(summary = "修改个人信息")
    @PostMapping("/modify")
    public AjaxResult modify(@RequestBody UpdateUserBody updateUserBody) {
        userService.modify(updateUserBody);
        return AjaxResult.ok("修改成功");
    }

    @SaCheckRole(RoleKey.社团管理员)
    @Operation(summary = "请出社团")
    @GetMapping("/kickOut/{userId}")
    public AjaxResult kickOut(@PathVariable("userId") Long userId) {
        userService.kickOut(userId);
        return AjaxResult.ok("请出成功");
    }
}
